/*
 * Created by lindaojiang.
 * Copyright© 2014.  Infor Technology Co.Ltd. All rights reserved.
 */

package com.abc.defg;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

public final class PhotoServlet extends HttpServlet {

    private static final boolean isDebug = true;
    private static final long serialVersionUID = 10L;
    public static final String WEB_ROOT_DIR = "/var/www";
    public static final Logger logger = Logger.getLogger(PhotoServlet.class);

    @Override
    public void init() throws ServletException {
        super.init();
        //String configFile = "log4j.properties";
        //PropertyConfigurator.configure(configFile);
        logger.info("Servlet init");
        if(isDebug)
            logger.setLevel(org.apache.log4j.Level.ALL);
        else
            logger.setLevel(org.apache.log4j.Level.WARN);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String dataRecv = req.getParameter("data");
		//postData需要用des解密
		try{
			String postData = DataCoding.decode(dataRecv.getBytes("UTF-8"), "XcKi0k89");
			logger.debug(postData);
		}
		
        JSONObject jsonRet = null;
        try {
            JSONObject jsonCmd = new JSONObject(postData);
            String command = jsonCmd.optString("command", "");
            if(command.contentEquals("commit")) {
                jsonRet = ...
            }
        }
        catch (JSONException e) {
            logger.error("doPost postData to JSONObject error" + e.getMessage());
        }

        if(jsonRet == null) {
            try{
                jsonRet = new JSONObject();
                jsonRet.put("code", -1);
                jsonRet.put("msg", "服务器错误");
            }
            catch (JSONException e){
                logger.error("doPost jsonRet error" + e.getMessage());
                jsonRet = new JSONObject();
            }
        }

        resp.setContentType("application/octet-stream");
        resp.setCharacterEncoding("UTF-8");

		string sendJson = jsonRet.toString();
		try{
			//des加密在发送
                String sendString = DataCoding.encode(sendJson, "XcKi0k89");
            }
            catch (Exception e) {
				logger.error("DataCoding encode error " + e.getMessage());
            }
        try{
            PrintWriter writer = resp.getWriter();
            writer.write(sendString);
        }
        catch (IOException e){
            logger.error("PrintWriter error " + e.getMessage());
        }
    }
}
